﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using DFYE.Web.Models;
using JULONG.WEB.Library;
using JULONG.WEB.Library.expViewResult;

namespace DFYE.Web.Controllers
{

    [LookStudtenLoginFilter]
    public class studentsController : Controller
    {
        //Login Page
        [HttpGet]
        public ActionResult Login() {
            return View();
        }
        //Login 
        [HttpPost]
        public ActionResult Login(string name ,string psw)
        {
            DataSession<entityDBContainer, student> db = new DataSession<entityDBContainer,student>();
            var user = db.get(d=>d.name==name && d.password==psw);
            if(user==null){return RedirectToAction("Login");}

            Session.Add("LookStudtenLoginUserID", user.ID);
            return RedirectToAction("index");

        }
        //家长留言板
        public ActionResult guestBook() {
            int studentID = (int)Session["LookStudtenLoginUserID"];
            DataSession<entityDBContainer, guestBook> db = new DataSession<entityDBContainer, guestBook>();
            ViewData.Model = db.query().Where(d => d.studentID == studentID);
            ViewBag.student = db.newSession<student>().get(d => d.ID == studentID);

            return View();
        }
        public ActionResult addGuestBook(string content)
        {
            if (!string.IsNullOrWhiteSpace(content)) {
                int studentID = (int)Session["LookStudtenLoginUserID"];

                    guestBook gb = new guestBook();

                    gb.studentID = studentID;
                    gb.datetime = DateTime.Now;
                    gb.able = true;
                    gb.content = content;
                    gb.ip = Request.UserHostAddress;

                    DataSession<entityDBContainer, guestBook> db = new DataSession<entityDBContainer, guestBook>();

                    gb.name = db.newSession<student>().get(d => d.ID == studentID).name;

                    db.insert(gb);

            }
            return RedirectToAction("guestBook");
        }

        public ActionResult replyGuestBook(int id,string content)
        {
            string _content = content.Replace("[div", "<div").Replace("[/div]", "</div>").Replace("\"]","\">").Replace("{datetime}",DateTime.Now.ToString("yyyy-MM-dd HH:mm"));
            

            int studentID = (int)Session["LookStudtenLoginUserID"];

            DataSession<entityDBContainer, guestBook> db = new DataSession<entityDBContainer, guestBook>();
            var gb = db.get(d => d.ID == id);
            gb.content += _content;
            gb.replyDatetime = DateTime.Now;
            gb.able = true;
            db.save();
            if (Request.IsAjaxRequest() == true)
            {
                return myJson.success(_content);
            }
            else
            {
                return RedirectToAction("guestBook");
            }
        }
        //
        // GET: /students/
        
        public ActionResult Index()
        {
            int studentID = (int)Session["LookStudtenLoginUserID"];

            
            //int studentID = 1;
            DataSession<entityDBContainer, student> db = new DataSession<entityDBContainer,student>();
            ViewData.Model = db.get(d => d.ID == studentID);
            return View();
        }
        public ActionResult Replay() {
            return View();
        }
        public ActionResult LoginOff() {
            Session.Remove("LookStudtenLoginUserID");
            return RedirectToAction("Login");
        }


    }
}
